//
//  ViewController.m
//  12-21
//
//  Created by blue on 15-12-21.
//  Copyright (c) 2015年 hyd. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
{
   
    UIView * view;
    CGFloat lastScale;
    CGFloat lastRotate;

}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
    view = [[UIView alloc]initWithFrame:CGRectMake(20, 200, 100, 100)];
    view.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view];
    
    //移动
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)];
    [view addGestureRecognizer:pan];
    
    //拉伸
    UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(onPinch:)];
    [view addGestureRecognizer:pinch];
    //旋转
    UIRotationGestureRecognizer * rot = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(onRot:)];
    [view addGestureRecognizer:rot];
    
    
    pinch.delegate = self;
    rot.delegate = self;
    lastRotate = 0;
    lastScale = 1.0;
    
}


- (void)onClick:(UIPanGestureRecognizer *)pan
{
    
    CGPoint p = [pan translationInView:self.view];
//    CGRect rect  = view.frame;
//    rect.origin.x += p.x;
//    rect.origin.y +=p.y;
//    pan.view.frame = rect;
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
    [pan setTranslation:CGPointZero inView:pan.view];
    
}

- (void)onPinch:(UIPinchGestureRecognizer *)pinch{
    CGFloat scale = pinch.scale - lastScale +1;
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale,scale);

    lastScale = pinch.scale;
    
}

- (void)onRot:(UIRotationGestureRecognizer *)rotate{
    
    CGFloat rot = rotate.rotation - lastRotate;
    
    rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rot);
    
    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
